package ;

import haxe.Http;
import haxe.Log;
import hxjson2.JSON;
import js.Dom;
import js.Lib;
import  js.JQuery;
import me.cunity.debug.Out;
import me.cunity.js.dom.Menu;
import me.cunity.js.dom.SubMenu;
import me.cunity.js.dom.Base;

/**
 * ...
 * @author Axel Huizinga axel@cunity.me
 */


class Main
{
	//static function _(sel:String):JQuery { return new JQuery(sel).andSelf(); }
	static function _(sel:String):JQuery { return new JQuery(sel); }
	
	var id:String;
	//var initialized:Bool = false;	
	var menuJsonObj:Dynamic;
	var menuObj:MenuObject;
	var menu:Menu;
	var menuRoot:SubMenu;
	var param:Hash<String>;
	public var submenuMargin:Int;
	
	
	public var win:Window;
	
	public function new(?parentContainer:HtmlDom) 
	{
		win = Lib.window;
		trace(win);
		if (parentContainer == null)
			parentContainer = win.document.body;
		if (parentContainer.id == '')
			parentContainer.id = 'menuRootContainer';
		var mainFrame = _("#mainFrame");
		trace(parentContainer.id + ':' + parentContainer.nodeName);
		//trace(mainFrame);
		var jWin:JQuery = JQuery.jQwin;	
		trace(jWin.width() + ' x ' + jWin.height());
		//trace(win.innerWidth + ' x ' + win.innerHeight);
		 param = new Hash();
		if (win.location.search != '')
		{
			trace('search:' + win.location.search);
			var search:String = win.location.search.substr(1);
			var pairs:Array<String> = search.split('&');
			
			for (pair in pairs)
			{
				var kV:Array<String> = pair.split('=');
				param.set(kV[0], kV.length == 2 ? kV[1] : '');
			}
			trace(param.toString());
			if (param.exists('xml'))
			{
				Util.convertXml(param.get('xml'));		
				return;
			}

			
		}
		id = param.exists('id') ? param.get('id') : 'main';
		//load menu datat from json file
		menuJsonObj = JSON.decode(Http.requestUrl('menu.json'));

		menuObj = Base.createMenuObject(menuJsonObj);
		menuObj.id = id;
		menuObj.isRoot = true;
		if (menuJsonObj.submenuMargin != null)
		{
			menuObj.submenuMargin =  Std.parseInt(menuJsonObj.submenuMargin);
			//Reflect.deleteField(menuJsonObj, 'submenuMargin');
		}
		else
			menuObj.submenuMargin = 8;		
		//trace(menuJsonObj.width);		trace(menuObj.width);
		//trace(Std.string(menuObj));
		//trace(Type.getEnum(menuObj.align) + ' enumConstructor:' + Type.enumConstructor(menuObj.align));
		//trace(menuObj.id + ':' + menuObj.width);
	
		menu = new Menu(menuObj, parentContainer);
		/*menuRoot = menu.menuRoot;
		trace(' align:' + menuRoot.align +':' + menuObj.align + ' pack:' + menuRoot.pack + ':' + menuObj.pack 
			+ ' class:' + menuRoot.className);
		layout(menuRoot, parentContainer);
		Out.dumpLayout(menuRoot.node);
		trace(menuRoot.node.nodeName + ':' + menuRoot.node.offsetWidth +' x ' + menuRoot.node.offsetHeight 
			+ ':' +menuRoot.node.parentNode.parentNode);
		//trace('<textarea rows="10" cols="80">' + menuRoot.node.parentNode.innerHTML + '</textarea>');
		Out.dumpLayout(menu.menuBox.node);*/
		//parentContainer.appendChild(mDom.menuRoot);
		
	}	
	
	public static function main()
	{
		Log.trace = Out._trace;
		//Out.traceTarget = DebugOutput.CONSOLE;
		new Main();
	}
}